package com.xianbeiying.app.ckgl.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.xianbeiying.app.ckgl.R;

import java.io.File;
import java.util.ArrayList;

/**
 * 选择图片
 */
public class GridSelectImageAdapter extends BaseAdapter {
    ArrayList<String> imgPaths;
    private Context mContext;
    private LayoutInflater inflater;
    int maxNum;
    public GridSelectImageAdapter(Context context, ArrayList<String> imgPaths, int maxNum) {
        this.imgPaths = imgPaths;
        this.mContext = context;
        this.maxNum = maxNum;
        inflater= LayoutInflater.from(mContext);
    }
    public int getMaxNum() {
        return maxNum;
    }

    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }
    @Override
    public int getCount() {
        if(imgPaths.size() < maxNum){
            return imgPaths.size() +1;
        }else{
            return maxNum;
        }
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_selected, parent, false);
            holder = new ViewHolder();
            holder.imgSelected=(ImageView) convertView.findViewById(R.id.img_selected);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(position < imgPaths.size()) {
            Glide.with(mContext)
                    .load(new File(imgPaths.get(position)))
                    .centerCrop()
                    .thumbnail(0.1f)
                    .into(holder.imgSelected);
//            ImageLoader.getInstance().displayImage(imgPaths.get(position),holder.imgSelected);

        }else if(position == imgPaths.size()){
            Glide.with(mContext)
                    .load(R.mipmap.chang)
                    .centerCrop()
                    .into(holder.imgSelected);
//            holder.imgSelected.setImageResource(R.mipmap.add_img);
        }

        return convertView;
    }
    class ViewHolder{
        ImageView imgSelected;
    }
}
